import os
import re
import sys
from lxml import etree
import requests
BASE_DIR = os.path.dirname(os.path.dirname(os.path.abspath(__file__)))
sys.path.insert(0, os.path.join(BASE_DIR, ""))
from Logs.log import log1
from Logs.remove_logs import remove_log
from db_sql import insert_db
from utils import plastic_urls, headers, pc_abs, pc, ab, pom
from datetime import date, datetime
from retrying import retry


@retry(stop_max_attempt_number=3)
def request(url):
    resp = requests.get(url, headers=headers)
    html = etree.HTML(resp.content.decode())
    return html


def datas(url):
    db_dict = {}
    timestamp = date.today().strftime('%Y%m%d')
    db_dict["source"] = "搜料网"
    db_dict["lower_price"] = ""
    db_dict["higher_price"] = ""
    db_dict["create_time"] = datetime.now().strftime('%Y/%m/%d %H:%M:%S')
    db_dict["price_date"] = date.today().strftime('%Y/%m/%d')
    if re.search(r"taiwanqimei-pc\+abs", url):
        db_dict["raw_type"] = pc_abs
        db_dict["id"] = timestamp + pc_abs
        db_dict["data-pname"] = 'WONDERLOY® PC-540(NC)'
    elif re.search(r"taiwanqimei-pc", url):
        db_dict["raw_type"] = pc
        db_dict["id"] = timestamp + pc
        db_dict["data-pname"] = "WONDERLITE® PC-110(NC)"
    elif re.search(r"taiwanqimei-abs", url):
        db_dict["raw_type"] = ab
        db_dict["id"] = timestamp + ab
        db_dict["data-pname"] = "POLYLAC® PA-757(NC)"
    elif re.search(r"ribenbaoli-pom", url):
        db_dict["raw_type"] = pom
        db_dict["id"] = timestamp + pom
        db_dict["data-pname"] = "DURACON® M90-44(CF2001)"
    else:
        print("暂不支持抓取其他类型塑料数据")
        return
    return db_dict


def parse(html, db_dict):
    main_label = html.xpath("//div[@data-pname='{}']".format(db_dict["data-pname"]))[0]
    price_unit = main_label.xpath(".//span[@class='price-unit']/text()")[0].strip()
    db_dict["avg_price"] = re.search(r"(\d+) (\D+)", price_unit).group(1)
    db_dict["unit"] = re.search(r"(\d+) (\D+)", price_unit).group(2).replace(" ", "")
    name = main_label.xpath("./@data-generic")[0].strip()
    model = main_label.xpath("./@data-pname")[0].split(" ")[1].strip()
    supplier = main_label.xpath("./@data-supplier")[0].strip()
    db_dict["raw_name"] = name + "/" + model + "/" + supplier
    db_dict["change_amount"] = ""
    db_dict["change_range"] = ""
    # price_date = html.xpath("//span[@class='marquee-times']/text()")[0]
    # db_dict["price_date"] = datetime.strptime(price_date, '%Y-%m-%d')
    return db_dict


def save(db_dict):
    insert_db(db_dict)


def run():
    urls = plastic_urls()
    for url in urls:
        db_dict = datas(url)
        if db_dict:
            try:
                html = request(url)
                db_dict = parse(html, db_dict)
            except Exception as e:
                log1.error("{}访问/数据提取失败:{}".format(url, e))
                continue
            try:
                save(db_dict)
            except Exception as e:
                log1.error("{}数据保存失败:{}".format(url, e))
    log1.info("塑料END!")


if __name__ == '__main__':
    if datetime.now().day == 1:
        remove_log()
    run()